package com.chuangjiangx.member.coupon.ddd.application;

import com.chuangjiangx.commons.BeanUtils;
import com.chuangjiangx.member.coupon.ddd.application.command.CreateCardCommand;
import com.chuangjiangx.member.coupon.ddd.application.command.CreateWxCardCommand;
import com.chuangjiangx.member.coupon.ddd.application.command.UpdateCardCommand;
import com.chuangjiangx.member.coupon.ddd.application.command.UpdateWxCardCommand;
import com.chuangjiangx.member.coupon.ddd.domain.model.*;
import com.chuangjiangx.member.coupon.ddd.domain.service.MbrCardDomainService;
import com.chuangjiangx.member.coupon.ddd.domain.service.command.CreateCard;
import com.chuangjiangx.member.coupon.ddd.domain.service.command.CreateWxCard;
import com.chuangjiangx.member.coupon.ddd.domain.service.command.UpdateCard;
import com.chuangjiangx.member.coupon.ddd.domain.service.command.UpdateWxCard;
import com.chuangjiangx.member.basic.ddd.domain.model.MerchantId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

/**
 * 会员卡application
 *
 * @author wp
 * @date 2018-08-30 13:53
 **/
@Component
public class MbrCardApplication {

    private final MbrCardDomainService mbrCardDomainService;

    @Autowired
    public MbrCardApplication(MbrCardDomainService mbrCardDomainService) {
        this.mbrCardDomainService = mbrCardDomainService;
    }

    /**
     * 添加会员基础信息
     * @param command
     */
    public void createCard(CreateCardCommand command){
        CreateCard createCard = new CreateCard();
        BeanUtils.convertBean(command,createCard,(co,cr)->{
            cr.setMerchantId(new MerchantId(co.getMerchantId()));
            cr.setGiftScore("".equals(command.getGiftScore())? BigDecimal.ZERO:new BigDecimal(command.getGiftScore()));
            cr.setCardCoverChoice(co.getCardCoverChoice()==null?null: CardCoverEnum.fromCode(co.getCardCoverChoice()));
        });
        mbrCardDomainService.createCard(createCard);
    }

    public void updateCard(UpdateCardCommand command){
        UpdateCard updateCard = new UpdateCard();
        BeanUtils.convertBean(command,updateCard,(co,up)->{
            up.setMbrConfigId(new MbrConfigId(co.getMbrConfigId()));
            up.setGiftScore("".equals(command.getGiftScore())? BigDecimal.ZERO:new BigDecimal(command.getGiftScore()));
            up.setCardCoverChoice(co.getCardCoverChoice()==null?null:CardCoverEnum.fromCode(co.getCardCoverChoice()));
        });
        mbrCardDomainService.updateCard(updateCard);
    }

    /**
     * 添加微信会员信息
     * @param command
     */
    public void createWxCard(CreateWxCardCommand command){
        CreateWxCard createWxCard = new CreateWxCard();
        BeanUtils.convertBean(command,createWxCard,(co,cr)->{
            cr.setMerchantId(new MerchantId(co.getMerchantId()));
            cr.setShowEquityList(co.getShowEquityList()==null?null: ShowEquityEnum.fromCodeList(co.getShowEquityList()));
            cr.setInterfaceSwitch(co.getInterfaceSwitch()==null?null: SwicthEnum.fromCode(co.getInterfaceSwitch()));
        });
        mbrCardDomainService.createWxCard(createWxCard);
    }

    /**
     * 更新微信会员信息
     * @param command
     */
    public void updateWxCard(UpdateWxCardCommand command){
        UpdateWxCard updateWxCard = new UpdateWxCard();
        BeanUtils.convertBean(command,updateWxCard,(co,up)->{
            up.setMbrWxCardId(new MbrWxCardId(co.getMbrWxCardId()));
            up.setInterfaceSwitch(co.getInterfaceSwitch()==null?null:SwicthEnum.fromCode(co.getInterfaceSwitch()));
            up.setShowEquityList(ShowEquityEnum.fromCodeList(co.getShowEquityList()));
        });
        mbrCardDomainService.updateWxCard(updateWxCard);
    }

    /**
     * 同步微信会员卡审核状态
     * @param merchantId
     * @return
     */
    public void syncWxStatus(Long merchantId){
         mbrCardDomainService.syncWxStatus(new MerchantId(merchantId));
    }

    /**
     * 微信会员卡审核通过
     * @param wxCardId
     */
    public void wxCardAuditSuccess(String wxCardId){
        mbrCardDomainService.wxCardAuditSuccess(wxCardId);
    }

    /**
     * 微信会员卡审核失败
     * @param wxCardId
     */
    public void wxCardAuditFail(String wxCardId,String refuseReason){
        mbrCardDomainService.wxCardAuditFail(wxCardId,refuseReason);
    }

    /**
     * 能否同步至微信
     * @param merchantId
     */
    public Integer canSyncWx(Long merchantId){
        return mbrCardDomainService.canSyncWx(new MerchantId(merchantId)).code;
    }

    /**
     * 更新同步微信开关
     * @param merchantId
     * @param wxSyncSwitch
     */
    public void updateSyncWxSwitch(Long merchantId,Integer wxSyncSwitch){
        mbrCardDomainService.updateSyncWxSwitch(new MerchantId(merchantId),SwicthEnum.fromCode(wxSyncSwitch));
    }

    /**
     * 判断能否领卡
     * @param
     */
    public String canGiftCard(String orderNumber,Long merchantId,String openId){
        return mbrCardDomainService.canGiftCard(orderNumber,new MerchantId(merchantId),openId);
    }
}
